<?php

declare(strict_types=1);

namespace App\AdminApi\Module\Admin\Api;

use App\AdminApi\AdminApiBasicsController;
use App\AdminApi\Module\Admin\Search\LoginLogSearch;
use App\AdminApi\Module\Admin\Service\LoginLogService;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\Guard\Auth;
use Zms\HyperfResponse\Response;

#[
    Controller(prefix: "/admin-api/admin-login-log", server: self::SERVER),
    Auth(guard: self::GUARD, label: ['后台管理'], app: self::APP)
]
class LoginLogController extends AdminApiBasicsController
{

    #[Inject]
    protected LoginLogService $service;

    #[
        GetMapping('list'),
        Auth(label: ['登录日志'], sign: "admin.login-log", describe: "查看登录日志", view: '/admin/login-log/index')
    ]
    public function list(): ResponseInterface
    {
        $search = new LoginLogSearch([
            'where' => [
                ...$this->request->inputs(['os', 'ip', 'browser', 'time']),
                'user_id' => $this->request->input('user_id') ?: $this->dataAuth(),
            ],
            'page'=>$this->page(),
            'order' => $this->orderBy('id DESC'),
            'size' => $this->pageSize(),
            'with' => [
                'user' => function (HasOne $hasOne) {
                    $hasOne->select(['id', 'nickname', 'account']);
                }
            ]
        ]);
        return Response::Api()->success(data: $search->search());
    }

}